﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Data.Filtering;
using DevExpress.XtraEditors;
using BLL;
using Model;
using POS.Framework;
using POS;
using System.Threading;
using Common;

namespace Cofco.login
{
    public partial class frmLogin : DevExpress.XtraEditors.XtraForm
    {
        public frmMain frmmain = null;
        public frmLogin()
        {
            InitializeComponent();
        }

        public LoginRightMenu LoginRightMenuUC
        {
            get { return this.loginRightMenu; }
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (CheckUiValue() == false) return;
            S_User user = DatabaseSchema.Session.FindObject<S_User>(DevExpress.Data.Filtering.CriteriaOperator.Parse("LoginName=?", this.txtLoginName.Text.Trim()));
            if (user == null)
            {
                MsgBox.ShowWarning("不存在的登录帐号！");
                this.txtLoginName.Text = string.Empty;
                this.txtPassWord.Text = string.Empty;
                this.txtLoginName.Select();
                return;
            }
            if (user.Password.ToLower() != MD5.FunEncrypt(this.txtPassWord.Text.Trim().ToLower()))
            {
                MsgBox.ShowWarning("输入登录密码不正确！");
                this.txtPassWord.Text = string.Empty;
                this.txtPassWord.Select();
                return;
            }
            MemberInfo.StoreCode = "LF01";
            MemberInfo.Store = DatabaseSchema.Session.FindObject<B_Store>(CriteriaOperator.Parse("StoreCode=?", MemberInfo.StoreCode));
            MemberInfo.User = user;
            S_UserPosSetUp upsu = DatabaseSchema.Session.GetObjectByKey<S_UserPosSetUp>(user.UserID);
            if (upsu == null)
            {
                upsu = new S_UserPosSetUp(DatabaseSchema.Session);
                upsu.UserID = MemberInfo.User.UserID;
                upsu.StoreSaleDetailMerge = false;
                upsu.DeliverySaleDetailMerge = false;
                upsu.Sort = 1;
                upsu.Save();
            }
            MemberInfo.UserPosSetUp = upsu;
            this.ParentForm.Hide();

            frmmain = new frmMain(this.ParentForm as Login);
            frmmain.Show();
            this.txtLoginName.Text = string.Empty;
            this.txtPassWord.Text = string.Empty;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.ParentForm.Close();
        }

        bool CheckUiValue()
        {
            if (string.IsNullOrEmpty(this.txtLoginName.Text.Trim()))
            {
                MsgBox.ShowWarning("请输入登录帐号！");
                this.txtLoginName.Select();
                this.txtPassWord.Text = string.Empty;
                return false;
            }
            if (string.IsNullOrEmpty(this.txtPassWord.Text.Trim()))
            {
                MsgBox.ShowWarning("请输入登录密码！");
                this.txtPassWord.Select();
                return false;
            }
            return true;
        }
    }
}